/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.domain.content;

import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.EntityConstants;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.CatalogBinding;
import han.bifang.gwt.client.common.entity.HttpBinding;
import han.bifang.gwt.client.common.entity.JmsBinding;
import han.bifang.gwt.client.common.entity.JmsBindingWrapper;
import han.bifang.gwt.client.common.entity.NormalJms;
import han.bifang.gwt.client.common.entity.ServiceCatalog;
import han.bifang.gwt.client.common.util.StringUtils;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.domain.DomainContentConstants;
import han.bifang.gwt.client.domain.DomainMessages;
import han.bifang.gwt.client.domain.tree.DomainTreeItem;

import java.util.ArrayList;
import java.util.HashMap;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;

/**
 * 
 * @Author Kent
 */
public class CreateOrModifyCatalogContent2 extends Content {

	private DomainMessages cons;
	private BifangGWTServiceAsync service;
	private ServiceCatalog sc = null;
	private VerticalPanel p = null;
	private String url;
	private ArrayList bindings;
	private TextBox queryUrl, monitorUrl, securityUrl, jmsServerUrl;
	//private PasswordTextBox jmsPass;
	private String parentId;
	private boolean isCreate;
	private boolean urlChanged;

	private AsyncCallback<ResponseObject> cb1 = new AsyncCallback<ResponseObject>() {

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());

		}

		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}

			if (isCreate) {
				ArrayList ret = (ArrayList) res.getResponse();
				DomainTreeItem c = new DomainTreeItem();
				c.setId((String) ret.get(0));
				c.setHTML(sc.getName());
				c.setType(EntityConstants.TREE_DOMAIN);
				c.setSubType(EntityConstants.TREE_DOMAIN_CATALOG);
				c.setKey((String) ret.get(1));
				HashMap m = new HashMap();
				m.put("item", c);
				m.put("domainId", sc.getDomainId());
				listener.onContentChanged(DomainContentConstants.CREATE_CAT2,
						DomainContentConstants.DOMAIN_DETAIL, m);
			} else {
				HashMap m = new HashMap();
				m.put("name", sc.getName());
				listener.onContentChanged(DomainContentConstants.MODIFY_CAT2,
						DomainContentConstants.CAT_DETAIL, m);
			}

		}
	};

	/**
	 * 
	 */
	private AsyncCallback<ResponseObject> cb = new AsyncCallback<ResponseObject>() {

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());

		}

		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}

			bindings = (ArrayList) res.getResponse();
			for (int i = 0, j = bindings.size(); i < j; i++) {
				CatalogBinding cb = (CatalogBinding) bindings.get(i);
				if (cb.getType().equals(EntityConstants.BINDING_HTTP)) {
					VerticalPanel p1 = new VerticalPanel();
					p1.setWidth("80%");
					p1.setBorderWidth(0);
					HttpBinding hb = (HttpBinding) cb.getBinding();
					HTML h = new HTML();
					TextBox tmp = null;
					if (cb.getName().equals(EntityConstants.CAT_BINDING_QUERY)) {
						h.setHTML(cons.queryBinding());
						queryUrl = new TextBox();
						queryUrl.setVisibleLength(80);
						queryUrl.setText(hb.getUrl());
						tmp = queryUrl;
					} else if (cb.getName().equals(
							EntityConstants.CAT_BINDING_MONITOR)) {
						h.setHTML(cons.monitorBinding());
						monitorUrl = new TextBox();
						monitorUrl.setVisibleLength(80);
						monitorUrl.setText(hb.getUrl());
						tmp = monitorUrl;
					} else if (cb.getName().equals(
							EntityConstants.CAT_BINDING_SECURITY)) {
						h.setHTML(cons.securityBinding());
						securityUrl = new TextBox();
						securityUrl.setVisibleLength(80);
						securityUrl.setText(hb.getUrl());
						tmp = securityUrl;

					}
					p1.add(h);
					Grid table = new Grid(3, 2);
					table.setWidth("100%");
					ColumnFormatter cf = table.getColumnFormatter();
					cf.setWidth(0, "20%");
					cf.setWidth(1, "50%");

					table.setCellPadding(1);
					table.setCellSpacing(1);
					table.setBorderWidth(1);

					CellFormatter ff = table.getCellFormatter();
					ff.setStyleName(0, 0, "tablecol");
					ff.setStyleName(1, 0, "tablecol");
					ff.setStyleName(2, 0, "tablecol");

					table.setHTML(0, 0, cons.name());
					table.setHTML(1, 0, cons.type());
					table.setHTML(2, 0, cons.url());

					table.setWidget(0, 1, h);
					table.setHTML(1, 1, cons.http());
					table.setWidget(2, 1, tmp);

					p1.add(table);
					p.add(p1);
				} else if (cb.getType().equals(EntityConstants.BINDING_JMS)) {
					VerticalPanel p1 = new VerticalPanel();
					p1.setWidth("80%");
					p1.setBorderWidth(0);
					JmsBindingWrapper w = (JmsBindingWrapper) cb.getBinding();
					JmsBinding jb = w.getJmsBinding();
					NormalJms info = (NormalJms)w.getInfo();
					
					Grid table = new Grid(10, 2);
					table.setWidth("100%");
					ColumnFormatter cf = table.getColumnFormatter();
					cf.setWidth(0, "20%");
					cf.setWidth(1, "50%");

					table.setCellPadding(1);
					table.setCellSpacing(1);
					table.setBorderWidth(1);

					CellFormatter ff = table.getCellFormatter();
					ff.setStyleName(0, 0, "tablecol");
					ff.setStyleName(1, 0, "tablecol");
					ff.setStyleName(2, 0, "tablecol");
					ff.setStyleName(3, 0, "tablecol");
					ff.setStyleName(4, 0, "tablecol");
					ff.setStyleName(5, 0, "tablecol");
					ff.setStyleName(6, 0, "tablecol");
					ff.setStyleName(7, 0, "tablecol");
					ff.setStyleName(8, 0, "tablecol");
					ff.setStyleName(9, 0, "tablecol");

					table.setHTML(0, 0, cons.name());
					table.setHTML(1, 0, cons.type());
					table.setHTML(2, 0, cons.jmsServer());
					table.setHTML(3, 0, cons.jmsServerVersion());					
					table.setHTML(4, 0, cons.jmsServerUrl());
					table.setHTML(5, 0, cons.jmsFac());
					table.setHTML(6, 0, cons.jmsDestination());
					table.setHTML(7, 0, cons.jmsType());
					table.setHTML(8, 0, cons.jmsUser());
					table.setHTML(9, 0, cons.jmsPass());

					HTML h = new HTML(cons.statBinding());
					p1.add(h);

					table.setWidget(0, 1, h);
					table.setHTML(1, 1, cons.jms());

					jmsServerUrl = new TextBox();
					jmsServerUrl.setVisibleLength(80);
					;
					jmsServerUrl.setText(info.getServerUrl());
					table.setWidget(2, 1, jmsServerUrl);
					table.setHTML(3, 1, jb.getServer());
					table.setHTML(4, 1, jb.getServerVersion());
					
					table.setHTML(5, 1, info.getFactoryUrl());
					table.setHTML(6, 1, info.getDestinationJndi());
					table.setHTML(7, 1, cons.jmsTypeQueue());
					//jmsUser = new TextBox();
					//jmsUser.setVisibleLength(32);
					//;
					//jmsUser.setText(jb.getUser());
					table.setHTML(8, 1, "&nbsp;");
					//jmsPass = new PasswordTextBox();
					//jmsPass.setVisibleLength(32);
					
					//jmsPass.setText(jb.getPassword());
					table.setHTML(9, 1, "&nbsp;");
					p1.add(table);
					p.add(p1);

				}

			}

			Button b = null;
			if (isCreate) {
				b = new Button(cons.create());
			} else {
				b = new Button(cons.confirm());
			}
			b.addClickListener(new ClickListener() {

				public void onClick(Widget sender) {
					if (!validate()) {
						return;
					}
					for (int i = 0, j = bindings.size(); i < j; i++) {
						CatalogBinding cb = (CatalogBinding) bindings.get(i);
						if (cb.getName().equals(
								EntityConstants.CAT_BINDING_QUERY)) {
							HttpBinding b = (HttpBinding) cb.getBinding();
							b.setUrl(queryUrl.getText());
						} else if (cb.getName().equals(
								EntityConstants.CAT_BINDING_MONITOR)) {
							HttpBinding b = (HttpBinding) cb.getBinding();
							b.setUrl(monitorUrl.getText());
						} else if (cb.getName().equals(
								EntityConstants.CAT_BINDING_SECURITY)) {
							HttpBinding b = (HttpBinding) cb.getBinding();
							b.setUrl(securityUrl.getText());
						} else if (cb.getName().equals(
								EntityConstants.CAT_BINDING_STATISTICS)) {
							JmsBindingWrapper w = (JmsBindingWrapper) cb.getBinding();
//							Window.alert(w.getJmsBinding().getServer());
//							Window.alert(w.getJmsBinding().getVersion());
							//jb.setPassword(jmsPass.getText());
							//jb.setUser(jmsUser.getText());
							NormalJms info = (NormalJms)w.getInfo();
							info.setServerUrl(jmsServerUrl.getText());

						}
					}

					RequestObject req = new RequestObject();
					if (isCreate) {
						req
								.setAction("han.bifang.gwt.server.action.domain.CreateCatalogAction");
						HashMap map = new HashMap();
						map.put("parentTreeId", parentId);
						map.put("catalog", sc);
						map.put("bindings", bindings);
						req.setRequest(map);
					} else {
						req
								.setAction("han.bifang.gwt.server.action.domain.ModifyCatalogAction");
						HashMap map = new HashMap();

						map.put("catalog", sc);
						if (urlChanged) {
							map.put("bindings", bindings);
						}
						req.setRequest(map);

					}
					service.call(req, cb1);

				}

			});
			p.add(b);

		}

	};

	public CreateOrModifyCatalogContent2(DomainMessages cons,
			BifangGWTServiceAsync service, HashMap params) {
		this.cons = cons;
		this.service = service;
		this.sc = (ServiceCatalog) params.get("cat");
		this.url = (String) params.get("url");
		this.parentId = (String) params.get("parentTreeId");
		this.isCreate = (Boolean) params.get("create");
		this.urlChanged = params.get("urlChanged") == null ? false
				: (Boolean) params.get("urlChanged");
		// Window.alert("" + urlChanged);
		p = new VerticalPanel();
		p.setWidth("100%");
		p.setSpacing(20);
		HTML h = new HTML(cons.catalogBinding());
		p.add(h);
		initWidget(p);

	}

	public void onLoad() {
		RequestObject req = new RequestObject();
		if (isCreate) {
			req
					.setAction("han.bifang.gwt.server.action.domain.GetCatalogBindingfromUrlAction");
			
			ArrayList<String> p = new ArrayList<String>();
			p.add(url);
			p.add(sc.getAppServer());
			p.add(sc.getServerVersion());
			req.setRequest(p);
		} else {
			if (urlChanged) {
				req
						.setAction("han.bifang.gwt.server.action.domain.GetCatalogBindingfromUrlAction");
				ArrayList<String> p = new ArrayList<String>();
				p.add(url);
				p.add(sc.getAppServer());
				p.add(sc.getServerVersion());
				req.setRequest(p);
			} else {
				req
						.setAction("han.bifang.gwt.server.action.domain.GetCatalogBindingfromDBAction");
				req.setRequest(sc.getId());
			}
		}
		service.call(req, cb);

	}

	private boolean validate() {
		if (!StringUtils.checkLenght(queryUrl.getText(), 1, 1024)) {
			Window.alert(cons.lengthError(cons.queryUrl(), "1", "1024"));
			return false;
		}
		if (!StringUtils.checkLenght(monitorUrl.getText(), 1, 1024)) {
			Window.alert(cons.lengthError(cons.monitorUrl(), "1", "1024"));
			return false;

		}
		if (!StringUtils.checkLenght(securityUrl.getText(), 1, 1024)) {
			Window.alert(cons.lengthError(cons.securityUrl(), "1", "1024"));
			return false;

		}
		if (!StringUtils.checkLenght(jmsServerUrl.getText(), 1, 1024)) {
			Window.alert(cons.lengthError(cons.jmsServerUrl(), "1", "1024"));
			return false;

		}
		return true;

	}

}
